package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.service.DepartmentService;
import com.hdax.service.EmployeeInfoService;
import com.hdax.service.PlanService;
import com.mysql.cj.Session;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@EnableScheduling
@RequestMapping("/plan")
public class PlanController {

    @Autowired
    private PlanService planService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private EmployeeInfoService employeeInfoService;

    /**
     * 加载头信息
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/index")
    public String list(Model model,HttpSession session) {
        //判断登陆人是否是项目经理
        Employee employeeInfo=(Employee)session.getAttribute("loginUser");
        int id=employeeInfo.getEmpId();
        List<EmployeeInfo> employeeInfos=new ArrayList<>();
            List<Department> departmentList=departmentService.listAll(id);
            List<Plan> planList=new ArrayList<>();
            if (departmentList.size()==0){
                Plan p=new Plan();
                p.setEmpid(id);
                //查询时间
                planList=planService.CountDate(p);
                //根据id查询用户
                EmployeeInfo employeeInfo1 =employeeInfoService.findEmployeeInfoById(id);
                model.addAttribute("ename",employeeInfo1);
            }else{
                Plan p=new Plan();
                planList=planService.CountDate(p);
                if (id==1){
                    List<EmployeeInfo> employeeInfo1 =employeeInfoService.selectAll();
                    for (int i = 0; i < employeeInfo1.size(); i++) {
                        int eid=employeeInfo1.get(i).getId();
                        EmployeeInfo employeeInfo2=employeeInfoService.findEmployeeInfoById(eid);
                        employeeInfos.add(employeeInfo2);
                    }
                    model.addAttribute("empname",employeeInfos);
                }else{
                    //根据项目经理循环查询该部门的员工
                    for (int i = 0; i < departmentList.size(); i++) {
                        int did=departmentList.get(i).getId();
                        List<EmployeeInfo> employeeInfoList=employeeInfoService.emplist(did);
                        //获取员工的编号
                        for (int j = 0; j < employeeInfoList.size(); j++) {
                            int eid=employeeInfoList.get(j).getId();
                            EmployeeInfo employeeInfo1=employeeInfoService.findEmployeeInfoById(eid);
                            employeeInfos.add(employeeInfo1);
                        }
                    }
                }
                model.addAttribute("empname",employeeInfos);
            }
        model.addAttribute("pldate",planList);
        return "plan/PlanProfiles";
    }

    /**
     * 重新加载
     * @param page
     * @param limit
     * @param plan
     * @param id
     * @return
     */
    @PostMapping(path = "/list")
    @ResponseBody
    public Map<String,Object> list(int page,int limit,Plan plan, int id){
        if (id!=1){
            plan.setEmpid(id);
        }
        //判断登陆人是否是项目经理
        List<Department> departmentList=departmentService.listAll(id);
        List<Plan> planList=new ArrayList<>();
        int count=0;//总条数
        if (plan.getEmpid()!=null || plan.getStart()!=null){
            planList=planService.findAll((page-1)*limit,limit,plan);
            count=planList.size();
        }else{
            if (page-1!=0 || limit!=0){
                planList=planService.findAll((page-1)*limit,limit,plan);
                count=planService.count();
            }else if (departmentList.size()>0){
                //是部门经理
                //获取部门id
                for (int i = 0; i <departmentList.size() ; i++) {
                    //根据部门id查询所属员工
                    int pid=departmentList.get(i).getId();
                    List<EmployeeInfo> emplis=employeeInfoService.emplist(pid);
                    for (int j = 0; j < emplis.size(); j++) {
                        //获取员工id,查询个人计划
                        int empid=emplis.get(j).getId();
                        plan.setEmpid(empid);
                        List<Plan> p=planService.findAll((page-1)*limit,limit,plan);
                        for (int k = 0; k < p.size(); k++) {
                            int planid=p.get(k).getId();
                            planList.add(planService.selectByid(planid));
                            count++;
                        }
                    }
                }
            }else{
                //不是部门经理
                //根据员工id查询
                plan.setEmpid(id);
                List<Plan> p=planService.findAll((page-1)*limit,limit,plan);
                for (int k = 0; k < p.size(); k++) {
                    int planid=p.get(k).getId();
                    planList.add(planService.selectByid(planid));
                    count++;
                }
            }
        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        //查询总条数(该名称下)
        map.put("count",count);
        map.put("data",planList);
        return map;
    }

    /**
     * 接取任务
     * @param plan
     * @return
     */
    @PostMapping(path = "/jiequ")
    @ResponseBody
    public Map<String,Object> jiequ(Plan plan,int pid){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        //修改状态
        plan.setId(pid);
        plan.setState(10);
        int resule= planService.updateBystate(plan);
        if (resule>0){
            map.put("msg","接取成功");
            map.put("data","success");
        }else {
            map.put("msg","接取失败");
            map.put("data","fail");
        }
        return map;
    }

    /**
     * 审批
     */
    @GetMapping(path = "/edit")
    public ModelAndView edit(Integer id) {
        //根据ID查到员工信息
        Plan plan = planService.selectByid(id);

        ModelAndView mav = new ModelAndView("plan/edit");
        mav.addObject("employeeInfo",plan);
        return mav;
    }

    @PostMapping(path = "/update")
    @ResponseBody
    public Map<String,Object> update(Plan plan){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        int resule= planService.updateBystate(plan);
        if (resule>0){
            map.put("msg","审批成功");
            map.put("data","success");
        }else {
            map.put("msg","审批失败");
            map.put("data","fail");
        }
        return map;
    }

    /**
     * 添加操作1,获取属于该角色下的所有员工,并返回展示
     * @return
     */
    @GetMapping(path = "/stbyemp")
    public ModelAndView stbyemp(HttpSession session) {
        ModelAndView mav = new ModelAndView("plan/insert");
        /*
         *获取当前登陆人的id
         * 进行循环查询它属于那个部门的经理
         */
        Employee employeeInfo=(Employee)session.getAttribute("loginUser");
        int id=employeeInfo.getEmpId();
        List<Department> departmentList=departmentService.listAll(id);
        List<EmployeeInfo> planList=new ArrayList<>();
        int count=0;//总条数
        for (int i = 0; i <departmentList.size() ; i++) {
            //查询属于该部门的人
            int eid=departmentList.get(i).getId();
            if(eid==1){
                List<EmployeeInfo> employeeInfoList=employeeInfoService.selectAll();
                for (int j = 0; j < employeeInfoList.size(); j++) {
                    //获取名称
                    EmployeeInfo empname=employeeInfoService.findEmployeeInfoById(employeeInfoList.get(j).getId());
                    if (empname.getEmpName().equals("admin")){
                    }else{
                        planList.add(empname);
                    }
                }
            }else{
                List<EmployeeInfo> employeeInfoList=employeeInfoService.emplist(eid);
                //循环部门的人,查询姓名
                for (int j = 0; j < employeeInfoList.size(); j++) {
                    //获取名称
                    EmployeeInfo empname=employeeInfoService.findEmployeeInfoById(employeeInfoList.get(j).getId());
                    planList.add(empname);
                }
            }
        }

        mav.addObject("pname",planList);
        return mav;
    }

    /**
     * 添加个人任务
     * @param plan
     * @return
     */
    @PostMapping(path = "/insertplan")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> insertplan(Plan plan,HttpSession session){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        int resule=0;
        //赋予该状态的新值
        if (plan.getEmpid()<0){
            Employee employeeInfo=(Employee)session.getAttribute("loginUser");
            int id=employeeInfo.getEmpId();
            List<Department> departmentList=departmentService.listAll(id);
            int count=0;//总条数
            for (int i = 0; i <departmentList.size() ; i++) {
                //查询属于该部门的人
                int eid=departmentList.get(i).getId();
                List<EmployeeInfo> employeeInfoList=employeeInfoService.emplist(eid);
                //循环部门的人,查询姓名
                for (int j = 0; j < employeeInfoList.size(); j++) {
                    //获取名称
                    int employid=employeeInfoList.get(j).getId();
                    plan.setEmpid(employid);
                    planService.insertAll(plan);
                    resule++;
                }
            }
        }else{
            resule= planService.insertAll(plan);
        }
        if (resule>0){
            map.put("msg","添加成功");
            map.put("data","success");
        }else {
            map.put("msg","添加失败");
            map.put("data","fail");
        }
        return map;
    }
}
